/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：CouponRuleDaoImpl.java
 * History:
 *       May 11, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import net.sf.json.JSONObject;

import com.xteam.ecsystem.dao.ICouponRuleDao;
import com.xteam.ecsystem.entities.BaseCouponRule;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 购物券规则数据访问实现类
 * 
 * @author liuzhi
 *
 */
@Repository
public class CouponRuleDaoImpl extends DDLPrefix implements ICouponRuleDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	@SuppressWarnings("unchecked")
	public void delObject(String ids) {
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseCouponRule");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		List list = new ArrayList();
		String oId[] = ids.split(",");
		for (String id : oId) {
			Object obj = this.baseDao.get(clazz, id);
			if (obj != null) {
				list.add(obj);
			}
		}
		this.baseDao.deleteAll(list);
	}

	@SuppressWarnings("unchecked")
	public String getCouponRuleList(int pageIndex, int pageSize) {
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseCouponRule.REF + SQL_WHERE + "1=1";
			String countHQL = SQL_SELECT + "count(*)" + SQL_FROM + BaseCouponRule.REF + SQL_WHERE + "1=1";
			
			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.queryList(countHQL, hql, list
					.toArray(), pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void saveOrUpdateObject(Object obj, String id) {
		try {
			if (StringUtil.isNotNullOrEmpty(id)){
				this.baseDao.update(obj);
			}else {
				this.baseDao.save(obj);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public String getCouponRuleById(String id) {
		JSONObject jo = new JSONObject();
		
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseCouponRule.REF + SQL_WHERE + "1=1" + SQL_AND + "bcrId=?";
			list = this.baseDao.find(hql, new Object[]{id});
			
			jo = JSONObject.fromObject((BaseCouponRule)list.get(0));

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	@SuppressWarnings("unchecked")
	public void updateCouponRuleStateById(String id) {
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseCouponRule");
			
			BaseCouponRule bcr = (BaseCouponRule)this.baseDao.get(clazz, id);
			if(bcr.getBcrState() == 1){
				bcr.setBcrState(0);
			}else{
				bcr.setBcrState(1);
			}
			
			this.baseDao.saveOrUpdate(bcr);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
}
